package Utils;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.util.Log;

import net.ck.hzq.R;

import java.util.List;

import Bean.AchievementBean;
import Bean.AchievementParamBean;
import Bean.AchievementParamBean1;
import Bean.AddBankCardBean;
import Bean.AddBankCardParamBean;
import Bean.AddMPOSBean;
import Bean.AddMPOSParamBean;
import Bean.AddOrderBean;
import Bean.AddOrderParamBean;
import Bean.AddaddressBean;
import Bean.AddaddressParamBean;
import Bean.AddressBean;
import Bean.AddressParamBean;
import Bean.AliPayBean;
import Bean.AliPayParamBean;
import Bean.AllyBean;
import Bean.AllyParamBean;
import Bean.AreaBean;
import Bean.AuthBean;
import Bean.AuthParamBean;
import Bean.AuthenticationBean;
import Bean.AuthenticationParamBean;
import Bean.CancelBank;
import Bean.CancelOrder;
import Bean.CancelParamOrder;
import Bean.ChangePasswordBean;
import Bean.ChangePasswordParamBean;
import Bean.ComplaintBean;
import Bean.ComplaintParamBean;
import Bean.EditAddressParamBean;
import Bean.EnterpriseassBean;
import Bean.EnterpriseassParamBean;
import Bean.FindAddressBean;
import Bean.ForwardBean;
import Bean.ForwardParamBean;
import Bean.GetBankcardBean;
import Bean.GetBankcardParamBean;
import Bean.GetMainActivityDataBean;
import Bean.GetMainActivityDataParamBean;
import Bean.HistoricalincomeBean;
import Bean.KnowledgeBean;
import Bean.KnowledgeParamBean;
import Bean.LearnMore;
import Bean.LoginBean;
import Bean.LoginParamBean;
import Bean.MPOSBean;
import Bean.MPOSParamBean;
import Bean.MessagecenterBean;
import Bean.MessagecenterParamBean;
import Bean.ModifyPasswordBean;
import Bean.ModifyPasswordParamBean;
import Bean.MyearningsBean;
import Bean.MyearningsParamBean;
import Bean.OrderBean;
import Bean.OrderDetailsBean;
import Bean.OrderDetailsParamBean;
import Bean.OrderParamBean;
import Bean.POSSelectBean;
import Bean.POSSelectBean2;
import Bean.POSSelectParamBean;
import Bean.PhoneNumerBean;
import Bean.PhoneNumerParamBean;
import Bean.PresentRecordBean;
import Bean.PresentRecordParamBean;
import Bean.ProductBean;
import Bean.ProductParamBean;
import Bean.RegisterBean;
import Bean.RegisterParamBean;
import Bean.ResultBean;
import Bean.ReturnofincomeBean;
import Bean.SendPhoneCodeBean;
import Bean.SendPhoneCodeParamBean;
import Bean.SetDefaultAddressBean;
import Bean.SetDefaultAddressParamBean;
import Bean.ShareBean;
import Bean.ShareParamBean;
import Bean.UpdateBean;
import Bean.UpdateVersion;
import Bean.Updateapp;
import base.BaseBiz;
import coom.RetrofitHelp;
import dao.AddPosInput;
import dao.AllyRewardOutput;
import dao.BackAmountOutput;
import dao.BaseOutput;
import dao.ChangeBalanceCardInput;
import dao.CustomIdInput;
import dao.FlowDevicesInput;
import dao.GetUrlOutput;
import dao.HomeDataInput;
import dao.HomeDataOutput;
import dao.HomePageOutput;
import dao.MerchantTradeInput;
import dao.MerchantTradeOutput;
import dao.SubmitWorkOrderOutput;
import dao.TradeRecordOutput;
import dao.WorkOrderDetailInput;
import dao.WorkOrderDetailOutput;
import dao.WorkOrderHandleInput;
import dao.WorkOrderListOutput;
import model.PersonalCenterBean;
import model.PersonalCenterParamBean;
import model.PersonalIconBean;
import retrofit2.Call;
import ui.activity.profit.biz.ProfitBiz;

/**
 * Created by Administrator on 2018/4/16.
 */

public class HelloWordModel {

    private static HelloWordModel sInstance;
    private CoordinateService mCoordinateService;
    private static Context mContext;

    public static HelloWordModel getInstance(Context context) {
        if (null == sInstance) {
            mContext = context.getApplicationContext();
            sInstance = new HelloWordModel();
        }
        return sInstance;
    }

    private HelloWordModel() {
        mCoordinateService = (CoordinateService) SystemDatas.getInstance(mContext).create(CoordinateService.class);
    }

    public Call<OrderBean> getOrder(String url, OrderParamBean orderParamBean) {
        Call<OrderBean> resultCall = mCoordinateService.getOrder(url, orderParamBean);
        return resultCall;
    }

    public Call<LoginBean> login(String url, LoginParamBean loginParamBean) {
        Call<LoginBean> resultCall = mCoordinateService.login(url, loginParamBean);
        return resultCall;
    }

    public Call<CancelParamOrder> cancelOrder(String url, CancelOrder cancelOrder) {
        Call<CancelParamOrder> resultCall = mCoordinateService.cancelOrder(url, cancelOrder);
        return resultCall;
    }

    public Call<CancelParamOrder> cancelBank(String url, CancelBank cancelBank) {
        Call<CancelParamOrder> resultCall = mCoordinateService.cancelBank(url, cancelBank);
        return resultCall;
    }

    public Call<CancelParamOrder> confimOrder(String url, CancelOrder cancelOrder) {
        Call<CancelParamOrder> resultCall = mCoordinateService.confimOrder(url, cancelOrder);
        return resultCall;
    }

    public Call<OrderDetailsBean> selectOrderDetails(String url, OrderDetailsParamBean cancelOrder) {
        Call<OrderDetailsBean> resultCall = mCoordinateService.getOrderDetails(url, cancelOrder);
        return resultCall;
    }

    public Call<SendPhoneCodeBean> getPhoneCode_register(String url, SendPhoneCodeParamBean cancelOrder) {
        Call<SendPhoneCodeBean> resultCall = mCoordinateService.getPhoneCode_register(url, cancelOrder);
        return resultCall;
    }

    public Call<OrderDetailsBean> getPhoneCode_forget(String url, OrderDetailsParamBean cancelOrder) {
        Call<OrderDetailsBean> resultCall = mCoordinateService.getPhoneCode_forget(url, cancelOrder);
        return resultCall;
    }

    public Call<RegisterBean> register(String url, RegisterParamBean cancelOrder) {
        Call<RegisterBean> resultCall = mCoordinateService.register(url, cancelOrder);
        return resultCall;
    }

    public Call<ModifyPasswordBean> modifyPassword(String url, ModifyPasswordParamBean cancelOrder) {
        Call<ModifyPasswordBean> resultCall = mCoordinateService.modifypassword(url, cancelOrder);
        return resultCall;
    }

    public Call<MPOSBean> getPickupMachine(String url, MPOSParamBean cancelOrder) {
        Call<MPOSBean> resultCall = mCoordinateService.getPickupMachine(url, cancelOrder);
        return resultCall;
    }

    public Call<ProductBean> getProduct(String url, ProductParamBean cancelOrder) {
        Call<ProductBean> resultCall = mCoordinateService.getProduct(url, cancelOrder);
        return resultCall;
    }

    public Call<AddressBean> getCity(String url) {
        Call<AddressBean> resultCall = mCoordinateService.getCity(url);
        return resultCall;
    }

    public Call<AddaddressBean> addAddress(String url, AddaddressParamBean addaddressParamBean) {
        Call<AddaddressBean> resultCall = mCoordinateService.addAddress(url, addaddressParamBean);
        return resultCall;
    }


    public Call<AddaddressBean> editAddress(String url, EditAddressParamBean addaddressParamBean) {
        Call<AddaddressBean> resultCall = mCoordinateService.eidtAddress(url, addaddressParamBean);
        return resultCall;
    }

    public Call<MyearningsBean> getMyearnings(String url, MyearningsParamBean myearningsParamBean) {
        Call<MyearningsBean> resultCall = mCoordinateService.getmyearnings(url, myearningsParamBean);
        return resultCall;
    }

    public Call<HistoricalincomeBean> gethistoricalincome(String url, MyearningsParamBean myearningsParamBean) {
        Call<HistoricalincomeBean> resultCall = mCoordinateService.gethistoricalincome(url, myearningsParamBean);
        return resultCall;
    }

    public Call<FindAddressBean> getdefaultAddress(String url, MyearningsParamBean myearningsParamBean) {
        Call<FindAddressBean> resultCall = mCoordinateService.getdefaultAddress(url, myearningsParamBean);
        return resultCall;
    }

    public Call<ResultBean> deleteAddress(String url, AddressParamBean myearningsParamBean) {
        Call<ResultBean> resultCall = mCoordinateService.deleteaddress(url, myearningsParamBean);
        return resultCall;
    }

    public Call<SetDefaultAddressBean> setDefaultAddress(String url, SetDefaultAddressParamBean myearningsParamBean) {
        Call<SetDefaultAddressBean> resultCall = mCoordinateService.setDefaultAddress(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AllyBean> getAlly(String url, AllyParamBean myearningsParamBean) {
        Call<AllyBean> resultCall = mCoordinateService.getAlly(url, myearningsParamBean);
        return resultCall;
    }

    public Call<ComplaintBean> complaint(String url, ComplaintParamBean myearningsParamBean) {
        Call<ComplaintBean> resultCall = mCoordinateService.complaint(url, myearningsParamBean);
        return resultCall;
    }

    public Call<PersonalCenterBean> personal(String url, PersonalCenterParamBean myearningsParamBean) {
        Call<PersonalCenterBean> resultCall = mCoordinateService.personal(url, myearningsParamBean);
        return resultCall;
    }

    public Call<ChangePasswordBean> changepass(String url, ChangePasswordParamBean myearningsParamBean) {
        Call<ChangePasswordBean> resultCall = mCoordinateService.changepass(url, myearningsParamBean);
        return resultCall;
    }

    public Call<MessagecenterBean> getmessagecenter(String url, MessagecenterParamBean myearningsParamBean) {
        Call<MessagecenterBean> resultCall = mCoordinateService.getMessageCenter(url, myearningsParamBean);
        return resultCall;
    }

    public Call<POSSelectBean> getpos1(String url, POSSelectParamBean myearningsParamBean) {
        Call<POSSelectBean> resultCall = mCoordinateService.getPOS1(
                url, myearningsParamBean.getCustomId(), myearningsParamBean.getType());
        return resultCall;
    }

    public Call<POSSelectBean2> getpos2(String url, POSSelectParamBean myearningsParamBean) {
        Call<POSSelectBean2> resultCall = mCoordinateService.getPOS2(url, myearningsParamBean);
        return resultCall;
    }

    public Call<PhoneNumerBean> getphonecode(String url, PhoneNumerParamBean myearningsParamBean) {
        Call<PhoneNumerBean> resultCall = mCoordinateService.getphonecode(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AddMPOSBean> addMPOS(String url, AddMPOSParamBean myearningsParamBean) {
        Call<AddMPOSBean> resultCall = mCoordinateService.addMPOS(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AddOrderBean> addOrder(String url, AddOrderParamBean myearningsParamBean) {
        Call<AddOrderBean> resultCall = mCoordinateService.addOredr(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AliPayBean> AliPay(String url, AliPayParamBean myearningsParamBean) {
        Call<AliPayBean> resultCall = mCoordinateService.AliPay(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AchievementBean> getachievemtn(String url, AchievementParamBean myearningsParamBean) {
        Call<AchievementBean> resultCall = mCoordinateService.getAchievement(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AchievementBean> getachievemtn1(String url, AchievementParamBean1 myearningsParamBean) {
        Call<AchievementBean> resultCall = mCoordinateService.getAchievement1(url, myearningsParamBean);
        return resultCall;
    }


    public Call<KnowledgeBean> getKnowledge(String url, KnowledgeParamBean myearningsParamBean) {
        Call<KnowledgeBean> resultCall = mCoordinateService.getKnowledge(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AreaBean> getArea(String url, KnowledgeParamBean myearningsParamBean) {
        Call<AreaBean> resultCall = mCoordinateService.getArea(url, myearningsParamBean);
        return resultCall;
    }

    public Call<ShareBean> getShare(String url, ShareParamBean myearningsParamBean) {
        Call<ShareBean> resultCall = mCoordinateService.getShare(url, myearningsParamBean);
        return resultCall;
    }

    public Call<GetBankcardBean> getBankCard(String url, GetBankcardParamBean myearningsParamBean) {
        Call<GetBankcardBean> resultCall = mCoordinateService.getBankCard(url, myearningsParamBean);
        return resultCall;
    }

    public Call<ReturnofincomeBean> getReturnofincome(String url, GetBankcardParamBean myearningsParamBean) {
        Call<ReturnofincomeBean> resultCall = mCoordinateService.getReturnofincome(url, myearningsParamBean);
        return resultCall;
    }

    public Call<ForwardBean> getForward(String url, ForwardParamBean myearningsParamBean) {
        Call<ForwardBean> resultCall = mCoordinateService.getForward(url, myearningsParamBean);
        return resultCall;
    }

    public Call<PresentRecordBean> getPresentRecord(String url, PresentRecordParamBean myearningsParamBean) {
        Call<PresentRecordBean> resultCall = mCoordinateService.getPresentRecord(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AuthenticationBean> getAuthentication(String url, AuthenticationParamBean myearningsParamBean) {
        Call<AuthenticationBean> resultCall = mCoordinateService.getAuthentication(url, myearningsParamBean);
        return resultCall;
    }

    public Call<AddBankCardBean> addBancard(String url, AddBankCardParamBean myearningsParamBean) {
        Call<AddBankCardBean> resultCall = mCoordinateService.addBankcard(url, myearningsParamBean);
        return resultCall;
    }

    public Call<EnterpriseassBean> getEnterprise(String url, EnterpriseassParamBean myearningsParamBean) {
        Call<EnterpriseassBean> resultCall = mCoordinateService.GetEnterprise(url, myearningsParamBean);
        return resultCall;
    }

    public Call<Updateapp> updateapp(String url, UpdateVersion version) {
        Call<Updateapp> resultCall = mCoordinateService.updateapp(url, version);
        return resultCall;
    }

    public Call<GetMainActivityDataBean> getMainActivityData(String url, GetMainActivityDataParamBean version) {
        Call<GetMainActivityDataBean> resultCall = mCoordinateService.getMainActivityData(url, version);
        return resultCall;
    }


    /**
     * 相关问题(了解更多)
     */
    public Call<LearnMore> getLearnInfor() {
        Call<LearnMore> resultCall = mCoordinateService.getLearnMore();
        return resultCall;
    }

    /**
     * 首页信息new
     */
    public Call<BaseOutput<HomePageOutput>> getHomePage(HomeDataInput param) {
        Call<BaseOutput<HomePageOutput>> resultCall = mCoordinateService.getHomePage(param);
        return resultCall;
    }

    /**
     * 首页信息
     */
    public Call<BaseOutput<HomeDataOutput>> getHomeData(HomeDataInput param) {
        Call<BaseOutput<HomeDataOutput>> resultCall = mCoordinateService.getHomeData(param);
        return resultCall;
    }

    /**
     * 实名认证
     */
    public Call<AuthBean> auth(String url, AuthParamBean param) {
        Call<AuthBean> resultCall = mCoordinateService.auth(url, param);
        return resultCall;
    }

    /**
     * 传统 POS 开通
     */
    public Call<BaseOutput<Object>> addTraditionalPos(AddPosInput param) {
        Call<BaseOutput<Object>> resultCall = mCoordinateService.addTraditionalPos(param);
        return resultCall;
    }

    /**
     * 交易记录
     */
    public Call<BaseOutput<TradeRecordOutput>> getTradeRecord(CustomIdInput input) {
        return mCoordinateService.getTradeRecord(input);
    }

    /**
     * 商户明细详情
     */
    public Call<BaseOutput<MerchantTradeOutput>> getMerchantTrade(MerchantTradeInput input) {
        return mCoordinateService.getMerchantTrade(input);
    }

    /**
     * 划拨规则
     */
    public Call<BaseOutput<GetUrlOutput>> getFlowRule() {
        return mCoordinateService.getFlowRule();
    }

    /**
     * 设备划拨
     */
    public Call<BaseOutput<Object>> flowDevices(FlowDevicesInput input) {
        return mCoordinateService.flowDevices(input);
    }

    /**
     * 工单列表
     */
    public Call<BaseOutput<WorkOrderListOutput>> getWorkOrderList(CustomIdInput input) {
        return mCoordinateService.getWorkOrderList(input);
    }

    /**
     * 工单列表
     */
    public Call<BaseOutput<WorkOrderListOutput>> getWorkOrderListDone(CustomIdInput input) {
        return mCoordinateService.getWorkOrderListDone(input);
    }

    /**
     * 工单详情
     */
    public Call<BaseOutput<WorkOrderDetailOutput>> getWorkOrderDetail(WorkOrderDetailInput input) {
        return mCoordinateService.getWorkOrderDetail(input);
    }

    /**
     * 工单处理
     */
    public Call<SubmitWorkOrderOutput> handleWorkOrder(WorkOrderHandleInput input) {
        return mCoordinateService.handleWorkOrder(input);
    }

    /**
     * 交易证明模板
     */
    public Call<BaseOutput<GetUrlOutput>> getPayProveTemplate() {
        return mCoordinateService.getPayProveTemplate();
    }

    /**
     * 更换结算卡
     */
    public Call<BaseOutput<Object>> changeBalanceCard(ChangeBalanceCardInput input) {
        return mCoordinateService.changeBalanceCard(input);
    }

    /**
     * 分润明细 -- 某合伙人旗下分润列表
     */
    public Call<BaseOutput<AllyRewardOutput>> getAllyReward(CustomIdInput input) {
        return mCoordinateService.getAllyRewardList(input);
    }

    public Call<BaseOutput<BackAmountOutput>> getBackAmount(List<String> sns) {
        return mCoordinateService.getBackAmount(sns);
    }

    public void updateapp(final Context context, SharedPreferences sp) {

        ProfitBiz biz = new ProfitBiz();

        biz.updateversion(RetrofitHelp.BASE_URL + "api/version?terminal=1", new BaseBiz.Callback<UpdateBean>() {
            @Override
            public void onSuccess(UpdateBean updateBean) {
                Log.e("fjs",ApkversioncodeUtils.getVersionCode(mContext)+"");
                    if (!(ApkversioncodeUtils.getVersionCode(mContext)+"").equals(updateBean.getVersion())) {
                        showNoticeDialog(context, updateBean.getUrl(),updateBean.getLevel(),updateBean.getTip()+"");
                    }
            }

            @Override
            public void onFailure(UpdateBean updateBean) {

            }
        });
//        UpdateVersion updateVersion = new UpdateVersion();
////        updateVersion.setType(ApkversioncodeUtils.getVerName(mContext));
//        updateVersion.setType("2");
//        Call<Updateapp> callhello2 = HelloWordModel.getInstance(mContext).updateapp(SystemDatas.GetService_URL("updateapp"),
//                updateVersion);
//        callhello2.enqueue(new Callback<Updateapp>() {
//            @Override
//            public void onResponse(Call<Updateapp> call, retrofit2.Response<Updateapp> response) {
//                try {
//                    if (ApkversioncodeUtils.getVersionCode(mContext)<response.body().getData().getVersion().getVersion()) {
//                        showNoticeDialog(context, response.body().getData().getVersion().getUpdateUrl());
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//
//            @Override
//            public void onFailure(Call<Updateapp> call, Throwable t) {
//            }
//        });

    }

    private void showNoticeDialog(final Context context, final String downUrl,int level,String content) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.soft_update_title);
        builder.setMessage(content);
        if(level == 1){
            builder.setPositiveButton(R.string.soft_update_updatebtn,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
                            showDownloadDialog(context, downUrl);
                        }
                    });
        }else {
            builder.setPositiveButton(R.string.soft_update_updatebtn,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
//                            dialog.dismiss();
                            showDownloadDialog(context, downUrl);
                        }
                    });
            builder.setNegativeButton(R.string.soft_update_later,
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
        }
        builder.setCancelable(false);
        Dialog noticeDialog = builder.create();
        noticeDialog.show();
    }

    private void showDownloadDialog(Context context, String downUrl) {
//        ToastUtils.shortToast("开始下载...");
//        Intent intent = new Intent(context, DownloadService.class);
//        intent.putExtra("url", downUrl);

        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(downUrl));
        context.startActivity(intent);

//        context.startService(intent);
    }

    /**
     * 用户上传头像
     *
     * @param url
     * @param personalIconBean
     * @return
     */
    public Call<PersonalCenterBean> personalIcon(String url, PersonalIconBean personalIconBean) {
        Call<PersonalCenterBean> resultCall = mCoordinateService.getPersonIcon(url, personalIconBean);
        return resultCall;
    }

}
